package com.example.exceldemo.controller;

import com.alibaba.excel.EasyExcel;
import com.example.exceldemo.entity.UserExcel;
import com.example.exceldemo.read.ExcelListener;
import com.example.exceldemo.service.ExportService;
import com.example.exceldemo.service.ImportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: yanhongwei
 * @Date: 2023/07/13 14:42:14
 * @Description: TODO
 */
@RestController
@RequestMapping("/importExcel")
@Api(tags="excel导入")
public class ReadController {

    @Autowired
    ImportService importService;

    @GetMapping("/importExcel01")
    @ApiOperation("导入方式一")
    public String exportExcel(){
        //实现excel读操作
        String filename = "C:\\Users\\admin\\Desktop\\用户信息.xlsx";
        EasyExcel.read(filename,UserExcel.class,new ExcelListener()).sheet().doRead();

        return "数据导入成功";
    }

    /**
     * 从Web中导入数据字典excel到数据库
     * @param file 变量名最好为file，否则前端可能出错
     * @return
     */
    @PostMapping("/importExcel02")
    @ApiOperation("导入方式二")
    public String importDictData(MultipartFile file){
        importService.importDictData(file);
        return "导入成功";
    }
}
